import {NotImplementedException} from '../utils/exception.js';
// LoaderStatus常量-加载器状态
export const LoaderStatus = {
    // kIdle 闲置
    kIdle: 0,
    // kConnecting 连接中
    kConnecting: 1,
    // kBuffering 缓冲中
    kBuffering: 2,
    // kError 出错
    kError: 3,
    // kComplete 完成
    kComplete: 4
};

// LoaderErrors-加载器错误
export const LoaderErrors = {
    // OK 成功
    OK: 'OK',
    // EXCEPTION 其他错误
    EXCEPTION: 'Exception',
    // HTTP_STATUS_CODE_INVALID HTTP 状态码错误
    HTTP_STATUS_CODE_INVALID: 'HttpStatusCodeInvalid',
    // CONNECTING_TIMEOUT 连接超时
    CONNECTING_TIMEOUT: 'ConnectingTimeout',
    // EARLY_EOF 过早结束
    EARLY_EOF: 'EarlyEof',
    // UNRECOVERABLE_EARLY_EOF 不可恢复的过早结束
    UNRECOVERABLE_EARLY_EOF: 'UnrecoverableEarlyEof'
};

/* Loader具有以下原型的回调:
 *     function onContentLengthKnown(contentLength: number): void
 *     function onURLRedirect(url: string): void
 *     function onDataArrival(chunk: ArrayBuffer, byteStart: number, receivedLength: number): void
 *     function onError(errorType: number, errorInfo: {code: number, msg: string}): void
 *     function onComplete(rangeFrom: number, rangeTo: number): void
 */
// BaseLoader类-数据加载器
export class BaseLoader {
    constructor(typeName) {
        // _type 加载器类型
        this._type = typeName || 'undefined';
        // _status 加载器状态
        this._status = LoaderStatus.kIdle;
        // _needStash 是否需要暂存区
        this._needStash = false;
        // callbacks
        // _onContentLengthKnown 已知内容长度事件处理函数
        this._onContentLengthKnown = null;
        // _onURLRedirect url地址重定向
        this._onURLRedirect = null;
        // _onDataArrival 数据抵达事件处理函数
        this._onDataArrival = null;
        // _onError 出错事件处理函数
        this._onError = null;
        // _onComplete 完成事件处理函数
        this._onComplete = null;
    }
    // 方法
    // 销毁加载器实例，就是将实例状态设为重置，四个事件处理函数设置为 null
    destroy() {
        this._status = LoaderStatus.kIdle;
        this._onContentLengthKnown = null;
        this._onURLRedirect = null;
        this._onDataArrival = null;
        this._onError = null;
        this._onComplete = null;
    }
    // 获取加载器运行状态
    isWorking() {
        // 根据加载器状态是连接中或者缓存中来判断
        return this._status === LoaderStatus.kConnecting || this._status === LoaderStatus.kBuffering;
    }
    // 打开数据源开始加载
    open(dataSource, range) {
        // 报错---延迟到子类实现
        throw new NotImplementedException('Unimplemented abstract function!');
    }
    // 终止加载
    abort() {
        // 报错---延迟到子类实现
        throw new NotImplementedException('Unimplemented abstract function!');
    }

    // 只读属性
    get type() {
        return this._type;
    }

    get status() {
        return this._status;
    }

    get needStashBuffer() {
        return this._needStash;
    }

    get onContentLengthKnown() {
        return this._onContentLengthKnown;
    }

    set onContentLengthKnown(callback) {
        this._onContentLengthKnown = callback;
    }

    get onURLRedirect() {
        return this._onURLRedirect;
    }

    set onURLRedirect(callback) {
        this._onURLRedirect = callback;
    }

    get onDataArrival() {
        return this._onDataArrival;
    }

    set onDataArrival(callback) {
        this._onDataArrival = callback;
    }

    get onError() {
        return this._onError;
    }

    set onError(callback) {
        this._onError = callback;
    }

    get onComplete() {
        return this._onComplete;
    }

    set onComplete(callback) {
        this._onComplete = callback;
    }


}